package cn.neusoft.yp.controller;

import cn.neusoft.yp.mapper.UserMapper;
import cn.neusoft.yp.pojo.Product;
import cn.neusoft.yp.pojo.User;
import cn.neusoft.yp.service.ProdService;
import cn.neusoft.yp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


/**
 * Created by gaosheng on 2017/4/29.
 */
@Controller
public class BackController {
    @Autowired
    private ProdService prodService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper ;
    /**
     * 登录后台
     * @return
     */
    @RequestMapping("backMian")
    public  String backMian(){
        return "back/manage" ;
    }

    /**
     * 后台商品列表
     * @return
     */
    @RequestMapping("backProdList")
    public String backProdList(Model model){
        List<Product> prodList =  prodService.findprod();
        model.addAttribute("prodList",prodList);
        return "../back/prod_list";
    }

    @RequestMapping("addProd")
    public String addProd(String name, Double price, String category, Integer pnum,String description, MultipartFile imgurl, HttpServletRequest context) throws IOException {
        Product prod = new Product();
        String imgName=imgurl.getOriginalFilename();
        String path="/upload";
        String code = Integer.toHexString(imgName.hashCode());
        char[] chs = code.toCharArray();
        for(int i=0;i<chs.length;i++){
            path=path+"/"+chs[i];
        }
        System.out.println(path+"/"+imgName);
        new File(context.getRealPath(path)).mkdirs();
        FileOutputStream fileOutputStream = new FileOutputStream(
                new File(context.getRealPath(path+"/"+imgName)));
        fileOutputStream.write(imgurl.getBytes());
        prod.setImgurl(path+"/"+imgName);
        String id= UUID.randomUUID().toString();
        prod.setId(id);
        prod.setName(name);
        prod.setPrice(price);
        prod.setCategory(category);
        prod.setPnum(pnum);
        prod.setDescription(description);

        prodService.addProd(prod);

        return "redirect:backProdList" ;
    }

    @RequestMapping("deleteProd")
    public String deleteProd(String id){
        prodService.deleteProd(id);
        return "redirect:backProdList" ;
    }
    @RequestMapping("backUserlist")
    public String backUserlist(Model model){
        List<User> userList =  userService.findAll() ;
        model.addAttribute("userList",userList);
        return "../back/user_list" ;
    }


    @RequestMapping("userPrivilege")
    public String backUserPrivilege(Model model,String id){
        String role = "admin" ;
        userMapper.updateRole(role,id);
        List<User> userList =  userService.findAll() ;
        model.addAttribute("userList",userList);
        return "../back/user_privilege" ;
    }
}
